﻿using CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouErYuanBLL;
using YouErYuanModels;

namespace YouErYuanManage.Controllers
{
    public class BaseDataController : Controller
    {
        /// <summary>
        /// 查询基础数据，食谱、课程、
        /// </summary>
        /// <returns></returns>
        [YouErYuanManage.Filter.IsLogin]
        public string GetData()
        {
            //List<object> list = new DictionaryBLL().GetDictionaryList();
            object dt= new DictionaryBLL().GetDictionaryList();
            //var data = new
            //{
            //    courseList = list[0],//课程
            //    repiceList = list[1],//食谱
            //    canTypeList = list[2],//餐类型
            //};
            return CommonMethod.FormatJsonObj("", dt);
        }

        /// <summary>
        /// 删除或新增
        /// </summary>
        /// <returns></returns>
        public string UpdateData()
        {
            Dictionary datas = new Dictionary();
            DictionaryBLL dic = new DictionaryBLL();
            //id集合
            string IDS = Request["id"] + "";
            //名称
            datas.Name = Request["name"] + "";
            //字段类型
            datas.Type = Request["type"] + "";
            //操作类型
            string action = Request["action"] + "";
            int count = 0;
            //删除
            if (action == "Del")
            {
                foreach (string item in IDS.Split('#'))
                {
                    if (item != "")
                    {
                        datas.Id = Convert.ToInt32(item);
                        //删除
                        count+=dic.UpdateData(datas, action);
                    }
                }
            }
            //新增
            else
            {
                switch (datas.Type)
                {
                    case "course":
                        datas.Pid = 10000;
                        break;
                    case "recipe":
                        datas.Pid = 20000;
                        break;
                    case "recipe_type":
                        datas.Pid = 30000;
                        break;
                }
                //得到同类型最大id号
                datas.Id = Convert.ToInt32(dic.getMaxId(datas)) + 1;
                //新增
                count=dic.UpdateData(datas, "add");
            }
            return CommonMethod.FormatJsonObj(count, "");
        }
    }
}